package com.wancaijinfu.widget;
import com.wancaijinfu.actitvity.R;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;

public class LoadingView {
	public static enum STATUS {
		LOADING, SUCCESS, FAILED
	}
	
	private boolean isVisiblity = true;
	private View mContentView;
	private ProgressBar loadingRefresh;
	private View errorView;
	private UIInterface infa;
	private STATUS mStatus;
	
	final AnimationListener animListener = new AnimationListener() {
		
		@Override
		public void onAnimationStart(Animation animation) {
			
		}
		
		@Override
		public void onAnimationRepeat(Animation animation) {
			
		}
		
		@Override
		public void onAnimationEnd(Animation animation) {
			setStatus(STATUS.SUCCESS);
			infa.addContext();
			if (infa != null) infa.addContext();
		}
	};
	
	public static LoadingView getInstance(Context context) {
		LoadingView loadingView = new LoadingView();
		loadingView.mContentView = LayoutInflater.from(context).inflate(R.layout.layout_base_loading, null);
		loadingView.loadingRefresh = (ProgressBar) loadingView.mContentView.findViewById(R.id.loading_refresh);
	//	loadingView.loadingRefresh.setProgressDrawable(new MaterialProgressDrawable(context, loadingView.loadingRefresh));
		loadingView.errorView = loadingView.mContentView.findViewById(R.id.network_error_refresh);
		loadingView.infa = context instanceof UIInterface ? (UIInterface) context : null;
		loadingView.setStatus(STATUS.LOADING);
		return loadingView;
	}
	
	public static LoadingView getInstance(Fragment context) {
		LoadingView loadingView = getInstance(context.getActivity());
		loadingView.infa = context instanceof UIInterface ? (UIInterface) context : null;
		return loadingView;
	}
	
	
	public void setStatus(STATUS status) {
		this.mStatus = status;
		
		if (status.equals(STATUS.LOADING)) {
			setLoadingVisiblity();
		}
		else if (status.equals(STATUS.FAILED)) {
			setNetVisiblity();
		} 
		else if (status.equals(STATUS.SUCCESS)) {
			setLoaingViewGone();
		}
	}
	
	private void setLoadingVisiblity() {
		loadingRefresh.setVisibility(View.VISIBLE);
		errorView.setVisibility(View.GONE);
	}
	
	private void setNetVisiblity() {
		loadingRefresh.setVisibility(View.GONE);
		errorView.setVisibility(View.VISIBLE);
	}
	
	public void loadingAnim() {
		Animation anim = AnimationUtils.loadAnimation(mContentView.getContext(), R.anim.list_item);
		anim.setAnimationListener(animListener);
		mContentView.startAnimation(anim);
	}
	
	private void setLoaingViewGone() {
		loadingRefresh.setVisibility(View.GONE);
		errorView.setVisibility(View.GONE);
		mContentView.setVisibility(View.GONE);
	}
	
	
	
	public View getContentView() {
		return mContentView;
	}
	
	public STATUS getStatus() {
		return mStatus;
	}
	
	public boolean isVisiblity() {
		return isVisiblity;
	}
	
	public void setVisiblity(boolean visibility) {
		this.isVisiblity = visibility;
	}
	
	public void setRefreshListener(View.OnClickListener refreshListener) {
		errorView.setOnClickListener(refreshListener);
	}

	
}
